<?php
require_once("include/auth.php");

$currentUser = current_user();
//required
$studentName = $_POST['student_name'];
$dob = $_POST['dob'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$perm_address = $_POST['perm_address'];
$contact_no = $_POST['contact_no'];
$email = $_POST['email'];
$major = $_POST['major'];
$degree = $_POST['degree'];
$tutor = $_POST['tutor'];


//optional - institution information
$name_first_inst = $_POST['name_first_inst'];
$name_second_inst = $_POST['name_second_inst'];
$name_third_inst = $_POST['name_third_inst'];

$first_major = $_POST['first_major'];
$second_major = $_POST['second_major'];
$third_major = $_POST['third_major'];

$first_degree = $_POST['first_degree'];
$second_degree = $_POST['second_degree'];
$third_degree = $_POST['third_degree'];

$first_grad = $_POST['first_grad'];
$second_grad = $_POST['second_grad'];
$third_grad = $_POST['third_grad'];

$first_gpa = $_POST['first_gpa'];
$second_gpa = $_POST['second_gpa'];
$third_gpa = $_POST['third_gpa'];

$classes = $_POST['classes'];
$errors = array();

//echo("$userName $password $confirmPassword $userType");

if ($studentName==""){
    array_push($errors, "1");
}
if ($dob == ""){
    array_push($errors,"2");
}

if ($gender == ""){
    array_push($errors,"3");
}
if ($address == ""){
    array_push($errors,"4");
}
if ($perm_address == ""){
    array_push($errors,"5");
}
if ($contact_no == ""){
    array_push($errors,"6");
}
if ($email == ""){
    array_push($errors,"7");
}
if ($tutor == "1"){
    updateTutorInfo($currentUser['Username'], true, $classes);
}
else{
    updateTutorInfo($currentUser['Username'], false, $classes);
}

//deletes out all education history
deleteEduHistory($currentUser['Username']);

for( $i = 0; $i < sizeof($name_first_inst); $i++ ) {
    if (($name_first_inst[$i] == "") && ($first_major[$i] == "") && ($first_degree[$i]=="") && ($first_grad[$i] =="") && ($first_gpa[$i] == "")) {
        // do nothing
    } else {
        $ed_history_add = setEduHistory($currentUser['Username'], $name_first_inst[$i], $first_major[$i], $first_degree[$i], $first_grad[$i], $first_gpa[$i]);
    }
}

if ( sizeof($errors) == 0){
    $user = updateUser($currentUser['Username'],$studentName,$dob, $gender, $address, $perm_address, $contact_no,
    $email);
    //echo(print_r($user));

    $student = updateStudent($currentUser['Username'], $major, $degree);
    }

    else{
        array_push($errors,"4");
    }
 //one or more errors exist
if ( sizeof($errors)> 0){
    $msg = join(',', $errors);
    redirect("personalinformation.php",array("errorMsg" =>$msg,"student_name"=>$studentName, "dob"=>$dob,
    "gender" => $gender,"address" => $address, "perm_address" => $perm_address, "contact_no" => $contact_no,
    "major" => $major, "degree" => $degree));
    //header("Location: create_account.php?errorMsg=$msg&userName=$userName&userType=$userType");
}
else{
    $msg = "Successfully updated your information!";
    redirect("personalinformation.php",array("successMsg" =>$msg,"student_name"=>$studentName, "dob"=>$dob,
        "gender" => $gender,"address" => $address, "perm_address" => $perm_address, "contact_no" => $contact_no,
        "major" => $major, "degree" => $degree));


}


?>